package com.example.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.dto.AppraiseRequest;
import com.example.bookstore.entity.Appraise;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.User;
import com.example.bookstore.mapper.AppraiseMapper;
import com.example.bookstore.mapper.BookMapper;
import com.example.bookstore.mapper.UserMapper;
import com.example.bookstore.service.AppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AppraiseServiceImpl extends ServiceImpl<AppraiseMapper, Appraise> implements AppraiseService {

    @Autowired
    AppraiseMapper appraiseMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public Result addAppraise(AppraiseRequest appraiseRequest) {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        List<Appraise> appraiseList = appraiseRequest.getItem();
        appraiseList.forEach(item->{
            Appraise appraise = new Appraise();
            appraise.setTime(format);
            appraise.setBookId(item.getBookId());
            appraise.setOrderId(appraiseRequest.getOrderId());
            appraise.setAppraiseText(item.getAppraiseText());
            appraise.setUserId(appraiseRequest.getUserId());
            appraiseMapper.insert(appraise);
        });
        return Result.success("评论添加成功");
    }
    @Override
    public PageResult<Appraise> getBookAppraise(int pageSize, int currentPage, int bookId) {
        Page<Appraise> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Appraise> qw=new LambdaQueryWrapper<>();
        qw.eq(Appraise::getBookId,bookId);
        Page<Appraise> page1 = appraiseMapper.selectPage(page, qw);
        List<Appraise> records = page1.getRecords();
        records.forEach(item->{
            User user = userMapper.selectById(item.getUserId());
            item.setRealName(user.getRealName());
            item.setUserImg(user.getUserImg());
        });
        long total = page1.getTotal();
        PageResult<Appraise> appraisePageResult = new PageResult<>(records, total, currentPage, pageSize);
        return appraisePageResult;
    }

    @Override
    public Result getAppraiseByOrder(int id) {
        LambdaQueryWrapper<Appraise> qw=new LambdaQueryWrapper<>();
        qw.eq(Appraise::getOrderId,id);
        List<Appraise> appraiseList = appraiseMapper.selectList(qw);
        appraiseList.forEach(item->{
            Book book = bookMapper.selectById(item.getBookId());
            item.setBookImg(book.getBookImg());
        });
        return Result.success(appraiseList);
    }
}
